<?php
/**
 * User:chendenan
 * Created On 2022/3/16 0016 20:49
 */
namespace app\admin\controller;
use app\admin\validate\Category as CategoryVal;
use app\common\business\Category as CategoryBus;
use app\common\lib\Arr AS ArrLib;
use think\facade\View;

class Category extends AdminBase{
    public function index(){
//        $pid = input("param.pid","0","intval");
//        $data = [
//            "pid" => $pid,
//        ];
        $data = [];
        try {
            $categorys = (new CategoryBus())->getLists($data,100);
        }catch (\Exception $e){
            $categorys = ArrLib::getPaginateDefaultData(100);
        }
        $result = ArrLib::list_to_tree($categorys['data']);
        $categorys['data'] = $result;
        return View::fetch("",[
            "categorys" => $categorys
        ]);
    }

    /**
     * 添加分类
     * @return string
     */
    public function add(){
        try {
            $categorys = (new CategoryBus())->getCategorys();
        }catch (\Exception $e){
            $categorys = [];
        }
        return View::fetch("",[
            "categorys"=> json_encode($categorys),
        ]);
    }

    /**
     * 保存分类
     * @return \think\response\Json
     */
    public function save(){
        $pid = input("param.pid","0","intval");
        $name = input("param.name","","trim");
        //参数校验
        $data = [
            'pid' => $pid,
            'name' => $name,
        ];
        $validate = new CategoryVal();
        if(!$validate->scene("add")->check($data)){
            return show($validate->getError(),config("status.error"));
        }
        try {
            $result = (new CategoryBus())->add($data);
        } catch (\Exception $e){
            trace($e->getMessage(),'error');
            return show($e->getMessage(),config("status.error"));
        }
        if($result){
            return show("OK",config("status.success"));
        }
        return show("新增分类失败",config("status.error"));
    }
    public function order(){
        $id = input("param.id","0","intval");
        $order=  input("param.order",0,"intval");
        if(!$id){
            return show("参数错误",config("status.success"));
        }
        $data = [
            'order' => $order,
        ];
        $validate = new CategoryVal();
        if(!$validate->scene("order")->check($data)){
            return show($validate->getError(),config("status.error"));
        }

        try{
            $res = (new CategoryBus())->updateById($id,$data);
        }catch (\Exception $e){
            trace($e->getMessage(),'error');
            return show($e->getMessage(),config("status.error"));
        }
        if($res){
            return show("排序成功",config("status.success"));
        }
        return show("排序失败",config("status.error"));
    }
    public function status(){
        $status = input("param.status",0,'intval');
        $id = input("param.id",0,"intval");
        if(!$id){
            return show("参数错误",config("status.success"));
        }
        $data = [
            'status' => $status,
        ];
        $validate = new CategoryVal();
        if(!$validate->scene("status")->check($data)){
            return show($validate->getError(),config("status.error"));
        }
        try{
            $res = (new CategoryBus())->updateById($id,$data);
        }catch (\Exception $e){
            return show($e->getMessage(),config("status.error"));
        }
        if($res){
            return show("状态更新成功",config("status.success"));
        }
        return show("状态更新失败",config("status.error"));
    }

    /**
     *
     * @return \think\response\View
     */
    public function dialog(){
        //获取一级分类数据
        $categorys = (new CategoryBus())->getByPid();
        return view('',[
            'categorys'=>json_encode($categorys),
        ]);
    }
    public function getByPid(){
        $pid = input("param.pid",0,"intval");
        $categorys = (new CategoryBus())->getByPid($pid);
        return show("OK",config("status.success"),$categorys);
    }
}